import { createRouter, createWebHistory } from "vue-router";
const routes = [
    {
        name: "home",
        path: '/',
        meta: { title: "首页" },
        component: () => import('../view/Home.vue'),
        redirect:'/userpart',
        children:[
            {
                name: "userpart",
                path: '/userpart',
                meta: { title: "用户管理" },
                component: () => import('../view/user/userpart.vue')
            },
            {
                name: "menu",
                path: '/menu',
                meta: { title: "菜单管理" },
                component: () => import('../view/menu/menu.vue')
            },
            {
                name: "DivisionalManagement",
                path: '/DivisionalManagement',
                meta: { title: "部门管理" },
                component: () => import('../view/DivisionalManagement/DivisionalManagement.vue')
            },
            {
                name: "myapprove",
                path: '/myapprove',
                meta: { title: "我的审批" },
                component: () => import('../view/user/myapprove.vue')
            },
            {
                name: "mychecking",
                path: '/mychecking',
                meta: { title: "我的考勤" },
                component: () => import('../view/checkingin/mychecking.vue')
            },
            {
                name: "checking",
                path: '/checking',
                meta: { title: "考勤总揽" },
                component: () => import('../view/checkingin/checking.vue')
            },
            {
                name: "managementq",
                path: '/managementq',
                meta: { title: "请假管理" },
                component: () => import('../view/management/managementq.vue')
            },
            {
                name: "managementx",
                path: '/managementx',
                meta: { title: "休假管理" },
                component: () => import('../view/management/managementx.vue')
            },
            {
                name: "managementj",
                path: '/managementj',
                meta: { title: "假期申请" },
                component: () => import('../view/management/managementj.vue')
            }
        ]
    },
    {
        name: "about",
        path: '/about',
        meta: { title: "about页" },
        component: () => import('../view/about.vue')
    },
    {
        name: "error",
        path: '/error',
        meta: { title: "error" },
        component: () => import('../components/error.vue')
    },
    {
        name: "login",
        path: '/login',
        meta: { title: "登录页面" },
        component: () => import('../view/login.vue')
    }
]

const router = createRouter({
  history: createWebHistory(),
  routes,
});
const hei = ['/','/about','/userpart']
router.beforeEach((to, from, next) => {
    if (to.matched.length === 0) {
        from.name ? next("/error") : next("/");
    } else {
        if(hei.includes(to.path)){
            if(sessionStorage.getItem('vuex')){
                next()
            }else{
                next('/login')
            }
        }else{
            next()
        }
    }
});

export default router;
